package elion.com.theElion.fragmentSeed;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import elion.com.elion.R;
import elion.com.theElion.util.Info;
import elion.vo.seed.Crop;
import elion.vo.seed.Family;

public class SeedFragment extends Fragment {

    public static final int SEED_CLASS = 1;
    public static final int SEED_GENUS = 2;
    public static final int CROP = 3;

    private FamilyAsy familyAsy = null;
    private CropAsy cropesAsy = null;

    private CropAdapter cropAdapter;

    private Spinner seedClassView;
    private Spinner seedGenusView;
    private ListView cropsView;

    /**
     * @param classes 科列表
     * @param genus 属列表
     * @param crops 具体植物列表
     */
    private List<Family> classes = null;
    private List<Family> genus = null;
    private List<Crop> crops = new ArrayList<>();

    //科名数组
    String[] classNames;
    //科id
    int[] classId;
    //属名数组
    String[] genusNames;
    //属id
    int[] genusId;
    //被选中属id
    int aGenusId;

    public SeedFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static SeedFragment newInstance() {
        SeedFragment fragment = new SeedFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_seed, container, false);
        System.out.println("laalal");
        findView(view);
        familyAsy = new FamilyAsy();
        familyAsy.setCallback(new FamilyAsy.CallBack() {
            @Override
            public void setList(List<Family> list) {
                classes = list;
                classNames = new String[classes.size()];
                classId = new int[classes.size()];
                int i = 0;
                for (Family f : classes) {
                    if (f != null) {
                        classNames[i] = f.getFamily_name();
                        classId[i] = f.getId();
                        i++;
                    }
                }
                classes.clear();
                ArrayAdapter<String> classAdapter;
                classAdapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, classNames);
                classAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
                seedClassView.setAdapter(classAdapter);
            }
        });
        //这里是科的url
        familyAsy.execute(Info.path_SelectSeed(SEED_CLASS, 1));
        onItemSelected();
        return view;
    }

    private void findView(View view) {
        seedClassView = (Spinner) view.findViewById(R.id.seed_class);
        seedGenusView = (Spinner) view.findViewById(R.id.seed_genus);
        cropsView = (ListView) view.findViewById(R.id.crop_list);

    }


    private void onItemSelected() {
        seedClassView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                familyAsy = new FamilyAsy();
                familyAsy.setCallback(new FamilyAsy.CallBack() {
                    @Override
                    public void setList(List<Family> list) {
                        genus = list;
                        genusNames = new String[genus.size()];
                        genusId = new int[genus.size()];
                        int i = 0;
                        for (Family f : genus) {
                            genusNames[i] = f.getFamily_name();
                            genusId[i] = f.getId();
                            i++;
                        }
                        ArrayAdapter<String> classAdapter;
                        classAdapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, genusNames);
                        classAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
                        seedGenusView.setAdapter(classAdapter);
                    }
                });
                familyAsy.execute(Info.path_SelectSeed(SEED_GENUS, classId[position]));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        seedGenusView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (genus != null && genus.size() != 0) {
                    aGenusId = genus.get(position).getId();
                }
                crops = new ArrayList<Crop>();
                cropesAsy = new CropAsy();
                cropesAsy.setCallback(new CropAsy.CallBack() {
                    @Override
                    public void setList(List<Crop> list) {
                        crops = list;
                        System.out.println("la" + "进入callback");

                        cropAdapter = new CropAdapter(list, getActivity());
                        cropsView.setAdapter(cropAdapter);
                    }
                });
                cropesAsy.execute(Info.path_SelectSeed(CROP, aGenusId));

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        cropsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Crop crop = crops.get(position);

                Intent intent = getActivity().getIntent();
                intent.putExtra("seed", crop);

                Fragment seedMain = new SeedMainFragment();
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fragment_container, seedMain);
                ft.addToBackStack(null);
                ft.commit();

            }
        });

    }


}
